﻿@using CleanArchitecture.Blazor.Application.Features.Products.Queries.Pagination
@inject IStringLocalizer<Products> L

<MudExpansionPanel @bind-Expanded="_advancedSearchExpanded"
                   Style="border-radius: var(--mud-default-borderradius) !important;"
                   Class="mud-elevation-25 pa-2 mb-3" Text="@ConstantString.AdvancedSearch">
    <MudGrid Spacing="2">
        <MudItem xs="12" sm="6" md="4">
            <MudTextField @bind-Value="TRequest.Name"
                          Placeholder="@L["Search by product name"]"
                          Clearable="true"
                          TextChanged="TextChanged">
            </MudTextField>
        </MudItem>
        <MudItem xs="12" sm="6" md="4">
            <PicklistAutocomplete Picklist="Picklist.Brand"
                                    T="string"
                                  Placeholder="@L["Search by brand"]"
                                  TextChanged="TextChanged"
                                  ResetValueOnEmptyText="true"
                                  @bind-Value="TRequest.Brand">
            </PicklistAutocomplete>
        </MudItem>
        <MudItem xs="12" sm="6" md="4">
            <PicklistAutocomplete Picklist="Picklist.Unit" T="string"
                                  Placeholder="@L["Search by unit"]"
                                  TextChanged="TextChanged"
                                  ResetValueOnEmptyText="true"
                                  @bind-Value="TRequest.Unit">
            </PicklistAutocomplete>
        </MudItem>
        <MudItem xs="12" sm="6" md="4">
            <MudNumericField Clearable="true" @bind-Value="TRequest.MinPrice"
                             Placeholder="@L["Search by minimum price"]"
                             TextChanged="TextChanged">
            </MudNumericField>
        </MudItem>
        <MudItem xs="12" sm="6" md="4">
            <MudNumericField @bind-Value="TRequest.MaxPrice"
                             Clearable="true"
                             Placeholder="@L["Search by maximum price"]"
                             TextChanged="TextChanged">
            </MudNumericField>
        </MudItem>
    </MudGrid>
</MudExpansionPanel>

@code
{
    [EditorRequired] [Parameter] public ProductsWithPaginationQuery TRequest { get; set; } = default!;
    [EditorRequired] [Parameter] public EventCallback<string> OnConditionChanged { get; set; }

    private bool _advancedSearchExpanded;

    private async Task TextChanged(string str)
    {
        if (_advancedSearchExpanded)
        {
            await OnConditionChanged.InvokeAsync(str);
        }
    }

}